/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Author:
 *     coolair.loveU2@gmail.com
 *******************************************************************************/

package org.skyfree.ghyll.tcard.core.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;

import org.eclipse.core.runtime.CoreException;
import org.skyfree.ghyll.tcard.core.IStoragePath;
import org.skyfree.ghyll.tcard.core.ITRepository;
import org.skyfree.ghyll.tcard.core.ITWorkpiece;

/**
 * This class ...
 */

public class TRepository extends AbstractTResource implements ITRepository
{
	HashMap<String,TreeSet<ITWorkpiece>> treeMapping;
	TreeMap<String,ITWorkpiece> wpMapping;
	
	public TRepository(IStoragePath path)
	{
		super(path);
		this.treeMapping = new HashMap<String,TreeSet<ITWorkpiece>>();
		this.wpMapping = new TreeMap<String,ITWorkpiece>();
	}

	@Override
	public ITWorkpiece getWorkpiece(String uniqueID)
	{
		return this.wpMapping.get(uniqueID);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void load() throws CoreException
	{
		String[] categorys = this.provider.getRepoCategorys();
		for(int i=0;i<categorys.length;i++)
		{
			this.reload(categorys[i]);
		}

	}

	@Override
	public void save() throws CoreException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void reload() throws CoreException
	{
		// TODO Auto-generated method stub
		
	}

	

	@Override
	public TreeSet<ITWorkpiece> getWorkPiece(String category) 
	{
		return this.treeMapping.get(category);
	}

	@Override
	public void createCategory(String name) throws Exception 
	{
		if(this.treeMapping.get(name)!=null)
			throw new Exception("Duplicated Category Name:	"+name);
		this.provider.createRepoCategory(name);
		this.treeMapping.put(name, new TreeSet<ITWorkpiece>());
	}

	@Override
	public void deleteCategory(String name, boolean force) throws Exception 
	{
		if(this.treeMapping.get(name)==null)
			throw new Exception("Category Not Exist:	"+name);
		this.provider.deleteRepoCategory(name);
		this.treeMapping.remove(name);
	}

	@Override
	public void reload(String category) throws CoreException 
	{
		String[] wps = this.provider.getWorkpiece(category);
		TreeSet<ITWorkpiece> wpList = new TreeSet<ITWorkpiece>();
		for(int i=0;i<wps.length;i++)
		{
			TWorkpiece wp = new TWorkpiece(this.path.append(category).append(wps[i]),wps[i]);
			wp.repository = this;
			wp.load();
			wpList.add(wp);
			this.wpMapping.put(wp.name, wp);
		}
		this.treeMapping.put(category, wpList);
	}

	@Override
	public boolean exist(String text) 
	{
		return this.treeMapping.containsKey(text);
	}

	@Override
	public Object[] getAllCategory() {
		Object[] all = this.treeMapping.keySet().toArray();
		return all ;
	}

	@Override
	public ArrayList<ITWorkpiece> searchWorkPiece(String partName) {
		Iterator<String> allWPNamesIT = this.wpMapping.descendingKeySet().descendingIterator();
		ArrayList<ITWorkpiece> re = new ArrayList<ITWorkpiece>();
		while(allWPNamesIT.hasNext())
		{
			String name = allWPNamesIT.next();
			if(name.contains(partName))
				re.add(this.wpMapping.get(name));
		}
		return re;
	}
	
}
